<?php

require 'db_config.php';

class Model {

    function list_forum() {
        global $con;
        $sql = "select * from models";
        $res = mysqli_query($con, $sql);
        $res = mysqli_fetch_all($res, MYSQLI_ASSOC);
        return json_encode($res);
    }

    function list_thread($m_id) {
        global $con;
        $sql = "select * from zt where m_id='$m_id'";
        $res = mysqli_query($con, $sql);
        $res = mysqli_fetch_all($res, MYSQLI_ASSOC);
        if (mysqli_affected_rows($con)) {
            return json_encode($res);
        } else {
            return 'Error！' . $sql;
        }
    }

    function add_forum($column_name, $column_intr) {
        global $con;
        $sql = "insert into models(name,description) values('$column_name','$column_intr')";
        mysqli_query($con, $sql);
        if (mysqli_affected_rows($con)>0) {
            return '添加版块成功！';
        } else {
            return '添加失败！' . $sql;
        }
    }

    function add_thread($title, $context,$name, $m_id) {
        global $con;
        $title= str_replace(' ', '', $title);
        $first_time= date("Y-m-d H:i");
        $last_time=$first_time;
        $sql = "insert into zt(m_id,title,context,name,first_time,last_time) values('$m_id','$title','$context','$name','$first_time','$last_time')";
        mysqli_query($con, $sql);
        if (mysqli_affected_rows($con)>0) {
            return '发帖成功！';
        } else {
            return '发帖失败！' . $sql;
        }
    }

}
